//
//  MyHealthLogViewController.m
//  安好
//
//  Created by 马晓良 on 14-11-17.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "MyHealthLogViewController.h"
#import "XHDrawerController.h"
#import "MainViewController.h"
#import "MyHealthLogWithPictureCell.h"
#import "MyHealthLogNoPictureCell.h"
#import "MyHealthTodayCell.h"
//#import "PerHeightIndicatorViewController.h"
//#import "TestResultsViewController.h"
#import "EditImportSignsViewController.h"
#import "EditViewController.h"
#import "MyHealthMoodViewController.h"
#import "MyHealthSearchBar.h"
#import "HealthMoodViewController.h"
#import "HealthRecordLogViewController.h"
#import "UserId.h"
#import "HealthDataSourse.h"
@interface MyHealthLogViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSInteger pages;
}
/**
 * 搜索框
 */
@property (strong, nonatomic) MyHealthSearchBar *searchBarView;
/**
 * tableView
 */
@property (strong, nonatomic) IBOutlet UITableView *MyHealthLogTableView;
/**
 * 页码
 */
@property (nonatomic, strong) NSString *numberOfPagesStr;

/**
 *  数据显示
 */
@property (nonatomic, strong) HealthDataSourse *dataSourceObjc;
/**
 *  存放以时间降序排列的的字典的value（以数组的形式保存）
 */
@property (nonatomic, strong) NSMutableArray *dicValue;
@property (nonatomic, strong) NSMutableArray *dicKeyArr;

/**
 *  判断当天的心情是否输入过了
 */
@property (nonatomic, assign) NSInteger todayMoodInt;
@end

@implementation MyHealthLogViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNaviBarAttribute];
    self.MyHealthLogTableView.dataSource = self;
    self.MyHealthLogTableView.delegate = self;
    self.MyHealthLogTableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"我的健康日志";
    
    
//#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
//    if (IOS8_OR_LATER) {
//        BOOL isFromHealth = [HKHealthStore isHealthDataAvailable];
//        if (isFromHealth == YES) {
//                HKHealthStore * hkHealthStore = [[HKHealthStore alloc]init];
//            
//
//               NSSet * sets = [NSSet setWithObjects:[HKObjectType workoutType],nil];
//
//            
//                [hkHealthStore requestAuthorizationToShareTypes:sets readTypes:sets completion:^(BOOL success, NSError *error) {
//                    NSLog( @"%hhd",success);
//                    NSLog( @"     %d",[hkHealthStore authorizationStatusForType:[HKObjectType workoutType]]);
//                    if ([hkHealthStore authorizationStatusForType:[HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex]] == HKAuthorizationStatusSharingAuthorized) {
//
//                    }
//                }];
//
//
//            
//        }
//    }
//#endif

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.dicKeyArr removeAllObjects];
    [self.dicValue removeAllObjects];
    pages = 1;
    [self _parsePersonalDataSource];
    [self.MyHealthLogTableView addFooterWithTarget:self action:@selector(_parsePersonalDataSource)];
}


- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self hiddenActionView];
    [self hiddenLabel];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 设置naviBar 并实现左右naviBar button方法 -
- (void)setNaviBarAttribute{
    /**
     *  设置左NaviBar和右naviBar
     */
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 23, 23);
    [leftButton setImage:[UIImage imageNamed:@"公用_返回"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(handleLeftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 30, 25);
    [rightButton setTitle:@"记录" forState:UIControlStateNormal];
  
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightButton addTarget:self action:@selector(handleRightButtonAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}


/**
 *  返回主页面
 */
- (void)handleLeftButtonAction{
    [self.navigationController popToRootViewControllerAnimated:YES];
}


/**
 *  完成(返回并传值)
 */
- (void)handleRightButtonAction{
    HealthRecordLogViewController *healthRecordLogVC = [[HealthRecordLogViewController alloc] init];
    [self.navigationController pushViewController:healthRecordLogVC animated:YES];
}

#pragma mark - 设置label的自适应高度 -
//  获取自适应label的高度 并让试图适应本高度
- (CGFloat)adaptiveLabelHeight:(NSString *)text wihtWight:(CGFloat)wight{
    UILabel *alablel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, wight, 29)];
    alablel.text = text;
    alablel.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13];
    CGSize size;
    if (kScreenWidth == 414) {
        size = CGSizeMake(wight, 66);
    }else{
        size = CGSizeMake(wight, 55);
    }
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13],NSFontAttributeName, nil];
    CGSize redSize = [alablel.text boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
    [alablel sizeToFit];
    if (kScreenWidth == 414) {
        if (redSize.height > 60.0f) {
            return 66.0f;
        }
    }else{
        if (redSize.height > 50.0f) {
            return 50.0f;
        }
    }
    return redSize.height;
}

#pragma mark - tableView的代理方法 -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *array = [NSArray array];
    array = self.dicValue[section];
    return array.count;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dicValue.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        if (kScreenWidth == 414) {
            return 83;
        }else{
            return 68;
        }
    }else{
        return 0.5;
    }
    return 0.5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.5;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        if (self.todayMoodInt == 1) {
            self.searchBarView.cameraButton.enabled = NO;
            [self.searchBarView.cameraButton setImage:[UIImage imageNamed:@"咨询_相机"] forState:UIControlStateNormal];
        }else{
            self.searchBarView.cameraButton.enabled = YES;
            [self.searchBarView.cameraButton setImage:[UIImage imageNamed:@"照相机"] forState:UIControlStateNormal];
        }
        return self.searchBarView;
    }else{
        return nil;
    }
}
    

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *objcArray = [NSArray array];
    
    objcArray = self.dicValue[indexPath.section];
    HealthDataSourse *healthObjc = [[HealthDataSourse alloc] init];
    // 获取model
    healthObjc = [objcArray objectAtIndex:indexPath.row];
    if (indexPath.row == 0) {
        if ([healthObjc.type intValue] == 1) {
            // 判断是否有图片
            if (healthObjc.pic.count == 0) {
                if (kScreenWidth == 414) {
                    return 57;
                }else{
                    return 47;
                }
            }else{
                // 这个是有图片的
                if (kScreenWidth == 414) {
                    return 78;
                }else{
                    return 65;
                }
            }
        }else{
            // 如果没有 type = 1 这一项
            if (kScreenWidth == 414) {
                return 57;
            }else{
                return 47;
            }
        }
    }else{
        if (kScreenWidth == 414) {
            return 57;
        }else{
            return 47;
        }
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // 带头像的cell
    MyHealthLogWithPictureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myHealthLogWithPictureCell"];
    MyHealthLogNoPictureCell *noPicCell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"myHealthLogNoPictureCell"];
    if (kScreenWidth == 414) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MyHealthLogWithPictureCell" owner:self options:nil] lastObject];
    }else{
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MyHealthLogWithPictureCell" owner:self options:nil] firstObject];
    }
    cell.contentView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    if (kScreenWidth == 414) {
        noPicCell = [[[NSBundle mainBundle] loadNibNamed:@"MyHealthLogNoPictureCell" owner:self options:nil] lastObject];
    }else{
        noPicCell = [[[NSBundle mainBundle] loadNibNamed:@"MyHealthLogNoPictureCell" owner:self options:nil] firstObject];
    }
    noPicCell.contentView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    [noPicCell.healthTestReaultJumpButton addTarget:self action:@selector(healthTestReaultJumpButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //为每个imageView添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandleAction:)];
    tap.numberOfTapsRequired = 1;
    cell.myHealthLogPicture.userInteractionEnabled = YES;
    [cell.clearView addGestureRecognizer:tap];
    NSArray *objcArray = [NSArray array];
    /**
     *  存放所有的解析出来的键值
     */
    MyNSLog("%@",[self getDate:self.dicKeyArr[indexPath.section]]);
    objcArray = self.dicValue[indexPath.section];
    HealthDataSourse *healthObjc = [[HealthDataSourse alloc] init];
    NSString *strss = [self setNewTime];
    healthObjc = [objcArray objectAtIndex:indexPath.row];
    
    MyNSLog("%@=====%@", strss,healthObjc.time);
    if (indexPath.row == 0) {
        if ([healthObjc.type intValue] == 1) {
            // 判断是否有图片
            if (healthObjc.pic.count == 0) {
                noPicCell.healthTestReaultLabel.text = healthObjc.descript;
                noPicCell.lineImageView.hidden = YES;
                if ([strss isEqualToString:healthObjc.time]) {
                    noPicCell.lineImageView.hidden = NO;
                    noPicCell.headerImageTimeView.hidden = YES;
                }else{
                    noPicCell.todayLabel.hidden = YES;
                    noPicCell.noPicDayLabel.text = [self setDay:[self getDate:self.dicKeyArr[indexPath.section]]];
                    noPicCell.noPicDayLabel.text = [self setDay:healthObjc.time];
                    NSString *str = [self setMonth:[self getDate:self.dicKeyArr[indexPath.section]]];
                    noPicCell.noPicMonthLabel.text = [NSString stringWithFormat:@"%@月",str];
                }
            }else{
                // 这个是有图片的
                cell.myHealthLogPicture.layer.masksToBounds = YES;
                cell.myHealthLogPicture.layer.cornerRadius = 3;
                [cell.myHealthLogPicture setImageWithURL:[NSURL URLWithString:[healthObjc.pic firstObject]] placeholderImage:[UIImage imageNamed:@"公用_默认图片"]];
                //  自适应高度
                CGFloat labelWight;
                if (kScreenWidth == 320) {
                    labelWight = 166;
                }else if (kScreenWidth == 375){
                    labelWight = 221;
                }else {
                    labelWight = 260;
                }
                CGFloat floats = [self adaptiveLabelHeight:healthObjc.descript wihtWight:labelWight];
                if (kScreenWidth == 414) {
                   cell.myStateDescription.frame = CGRectMake(153, 5, 147, floats);
                }else{
                    cell.myStateDescription.frame = CGRectMake(134, 5, 166, floats);
                }
                cell.myStateDescription.text = healthObjc.descript;
                if ([strss isEqualToString:healthObjc.time]) {
                    cell.viewAddlabel.hidden = YES;
                    MyNSLog("头一个图片的地址%@", [healthObjc.pic firstObject]);
                }else{
                    cell.todayLabel.hidden = YES;
                    cell.longLineImageView.hidden = YES;
                    cell.withPicDayLabel.text = [self setDay:[self getDate:self.dicKeyArr[indexPath.section]]];
                    cell.withPicMonthLabel.text = [NSString stringWithFormat:@"%@月",[self setMonth:[self getDate:self.dicKeyArr[indexPath.section]]]];
                }
                return cell;
            }
        }else{
            // 如果没有 type = 1 这一项
            noPicCell.healthTestReaultLabel.text = healthObjc.msg;
            noPicCell.lineImageView.hidden = YES;
            if ([strss isEqualToString:healthObjc.time]) {
                noPicCell.lineImageView.hidden = NO;
                noPicCell.headerImageTimeView.hidden = YES;
            }else{
                noPicCell.todayLabel.hidden = YES;
                noPicCell.noPicDayLabel.text = [self setDay:[self getDate:self.dicKeyArr[indexPath.section]]];
                NSString *str = [self setMonth:[self getDate:self.dicKeyArr[indexPath.section]]];
                noPicCell.noPicMonthLabel.text = [NSString stringWithFormat:@"%@月",str];
            }
        }
    }else{
        noPicCell.healthTestReaultLabel.text = healthObjc.msg;
        noPicCell.headerImageTimeView.hidden = YES;
    }
    return noPicCell;
}

#pragma mark - cell中button的执行方法 -
- (void)healthTestReaultJumpButtonAction:(UIButton *)sender{
    // 获取父类的cell对象
    MyHealthLogNoPictureCell *cell = nil;
    UIView  *view = sender.superview;
    for (NSInteger i = 0; i < 1; ) {
        if ([view isKindOfClass:[MyHealthLogNoPictureCell class]]) {
            cell = (MyHealthLogNoPictureCell *)view;
            break;
        }else{
            view =view.superview;
        }
    }
    // 获取当前的位置
    NSIndexPath *indexPath = [self.MyHealthLogTableView indexPathForCell:cell];
    HealthDataSourse *dataHealth = [[HealthDataSourse alloc] init];
    NSMutableArray *arr = self.dicValue[indexPath.section];
    dataHealth = arr[indexPath.row];
    if ([dataHealth.type intValue] == 1) {
        //  描述
        MyHealthMoodViewController *myHealthMoodVC = [[MyHealthMoodViewController alloc] init];
        myHealthMoodVC.contentStr = dataHealth.descript;
        myHealthMoodVC.currentObjcDataLine = dataHealth.time;
        MyNSLog("当前的时间戳 == %@", self.dicKeyArr[indexPath.section]);
        [self.navigationController pushViewController:myHealthMoodVC animated:YES];
    }else if ([dataHealth.type intValue] == 2){
        //  视力
        EditImportSignsViewController *editImportSigndVC = [[EditImportSignsViewController alloc] init];
        editImportSigndVC.dataTimeStr = dataHealth.time;
        MyNSLog("===传过去的时间=== %@", editImportSigndVC.dataTimeStr);
        editImportSigndVC.naviTitleStr = [NSString stringWithFormat:@"视力"];
        [self.navigationController pushViewController:editImportSigndVC animated:YES];
        
    }else{
        NSString *str;
        if ([dataHealth.type intValue] == 3) {
            str = @"心率";
        }else if ([dataHealth.type intValue] == 4){
            str = @"记步记录";
        }else if ([dataHealth.type intValue] == 6){
            str = @"呼吸速率";
        }else if ([dataHealth.type intValue] == 7){
            str = @"体温";
        }else if ([dataHealth.type intValue] == 8){
            str = @"血压";
        }else if ([dataHealth.type intValue] == 9){
            str = @"餐前血糖";
        }else if ([dataHealth.type intValue] == 15){
            str = @"餐后血糖";
        }else if ([dataHealth.type intValue] == 12){
            str = @"身高";
        }else if ([dataHealth.type intValue] == 13){
            str = @"体重";
        }else if ([dataHealth.type intValue] == 14){
            str = @"BMI";
        }else if ([dataHealth.type intValue] == 11){
            str = @"柔韧性";
        }else if ([dataHealth.type intValue] == 10){
            str = @"挥拳";
        }else if ([dataHealth.type intValue] == 5){
            str = @"色觉";
        }
        EditImportSignsViewController *editImportSigndVC = [[EditImportSignsViewController alloc] init];
        editImportSigndVC.dataTimeStr = dataHealth.time;
        MyNSLog("===传过去的时间=== %@", editImportSigndVC.dataTimeStr);
        editImportSigndVC.naviTitleStr = str;
        MyNSLog("头  ==  %@", str);
        [self.navigationController pushViewController:editImportSigndVC animated:YES];
    }
}


- (void)cameraButtonAction:(UIButton *)sender{
    HealthMoodViewController *healthMoodVC = [[HealthMoodViewController alloc] init];
    [self.navigationController pushViewController:healthMoodVC animated:YES];
}

#pragma mark - 手势执行方法 -
//手势方法
- (void)tapHandleAction:(UIGestureRecognizer *)sender{
    CGPoint tmpPointTouch = [sender locationInView:self.MyHealthLogTableView];
    NSIndexPath *indexPath = [self.MyHealthLogTableView indexPathForRowAtPoint:tmpPointTouch];
    HealthDataSourse *dataHealth = [[HealthDataSourse alloc] init];
    NSMutableArray *arr = self.dicValue[indexPath.section];
    dataHealth = arr[indexPath.row];
    //获取点击的位置
    MyHealthMoodViewController *myHealthMoodVC = [[MyHealthMoodViewController alloc] init];
    myHealthMoodVC.contentStr = dataHealth.descript;
    myHealthMoodVC.picArray = dataHealth.pic;
    myHealthMoodVC.currentObjcDataLine = [self getDate:self.dicKeyArr[indexPath.section]];
    [self.navigationController pushViewController:myHealthMoodVC animated:YES];
}

#pragma mark - 网路数据请求 -
- (void)_parsePersonalDataSource{
    [self showActionView];
    // 获取url地址
    NSString * postURL = [NSString stringWithFormat:@"%@user/daylog/",kBaseURL];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    if ([UserId shareUid].uidStr.length > 0) {
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        [parameters setObject:kPlatform forKey:@"platform"];
        [parameters setObject:kVersion forKey:@"version"];
        [parameters setObject:[NSString stringWithFormat:@"%ld",(long)pages] forKey:@"page"];
        [parameters setObject:[UserId shareUid].uidStr forKey:@"uid"];
        MyNSLog("%@",parameters);
        [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSNumber * statusNum = [responseObject objectForKey:@"status"];
            NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
            NSString * statusString = [numberFormatter stringFromNumber:statusNum];
            if ([statusString isEqualToString:@"0"]) {
                if (self.dicKeyArr.count > 0) {
                    [self.MyHealthLogTableView footerEndRefreshing];
                }else{
                    [self shouLabel:@"暂没有数据"];
                    [self.MyHealthLogTableView reloadData];
                }
            }else{
                NSDictionary * dataDic = [[responseObject objectForKey:@"data"] isEqual:[NSNull null]]?nil:[responseObject objectForKey:@"data"];
                if (dataDic != nil) {
                    pages++;
                    NSArray *array = [[dataDic allKeys]sortedArrayUsingSelector:@selector(compare:)];
                    NSMutableArray *keyArray = [NSMutableArray array];
                    //  降序排序
                    for (id obj in [array reverseObjectEnumerator]) {
                        [self.dicKeyArr addObject:obj];
                        [keyArray addObject:obj];
                    }
                    MyNSLog("=====%@", self.dicKeyArr);
                    MyNSLog("=====%@", dataDic);
                    //  封装
                    for (NSInteger i = 0; i < dataDic.count; i++) {
                        NSMutableArray *objcArr = [[NSMutableArray alloc] init];
                        for (NSDictionary *dic in [dataDic objectForKey:keyArray[i]]) {
                            HealthDataSourse *healthData = [[HealthDataSourse alloc] init];
                            healthData.descript = [dic objectForKey:@"description"];
                            [healthData setValuesForKeysWithDictionary:dic];
                            [objcArr addObject:healthData];
                        }
                        [self.dicValue addObject:objcArr];
                    }
                    if ([[self getDate:keyArray[0]] isEqualToString:[self setNewTime]]) {
                        HealthDataSourse *healthData = [[self.dicValue firstObject] firstObject];
                        if (healthData.descript != nil || healthData.pic != nil) {
                            // 今天心情已发送了
                            self.todayMoodInt = 1;
                        }else{
                            // 今天心情未发送
                            self.todayMoodInt = 0;
                        }
                    }else{
                        self.todayMoodInt = 0;
                    }
                }else if ([dataDic count] < 20){
                    [self.MyHealthLogTableView footerEndRefreshing];
                }else{
                    if (pages > 1) {
                        MyNSLog("没数据");
                    }
                }
             [self.MyHealthLogTableView reloadData];
            }
            [self hiddenActionView];
            [self.MyHealthLogTableView footerEndRefreshing];
        } failure:nil];
    }else{
        MyNSLog("没网");
    }
}

#pragma mark -  获取月和日  -
- (NSString *)setMonth:(NSString *)date{
    NSString *month = [date substringWithRange:NSMakeRange(5, 2)];
    return month;
}


- (NSString *)setDay:(NSString *)date{
    NSString *day = [date substringWithRange:NSMakeRange(8, 2)];
    return day;
}

#pragma mark - 懒加载 -
- (NSMutableArray *)dicKeyArr{
    if (_dicKeyArr == nil) {
        _dicKeyArr = [[NSMutableArray alloc] init];
    }
    return _dicKeyArr;
}

- (NSMutableArray *)dicValue{
    if (_dicValue == nil) {
        _dicValue = [[NSMutableArray alloc] init];
    }
    return _dicValue;
}

- (MyHealthSearchBar *)searchBarView{
    if (_searchBarView == nil) {
        if (kScreenWidth == 414) {
            _searchBarView = [[[NSBundle mainBundle] loadNibNamed:@"MyHealthSearchBar" owner:self options:nil] lastObject];
        }else{
            _searchBarView = [[[NSBundle mainBundle] loadNibNamed:@"MyHealthSearchBar" owner:self options:nil] firstObject];
        }
        _searchBarView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
        _searchBarView.buttonView.layer.masksToBounds = YES;
        _searchBarView.buttonView.layer.cornerRadius = 3.0f;
        [_searchBarView.cameraButton addTarget:self action:@selector(cameraButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchBarView;
}


#pragma mark - 获取的时间戳转换为时间 -
- (NSString *)getDate:(NSString *)string{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *theday = [NSDate dateWithTimeIntervalSince1970:[string doubleValue]];
    return [dateFormatter stringFromDate:theday];
}


#pragma mark - 获取当前的时间 -
- (NSString *)setNewTime{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSString *locationString=[formatter stringFromDate: [NSDate date]];
    return locationString;
}

//- (NSString *)setNewTime{
//    NSDate *now = [NSDate date];
//    NSCalendar *calendar = [NSCalendar currentCalendar];
//    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
//    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
//    int year = [dateComponent year];
//    int month = [dateComponent month];
//    int day = [dateComponent day];
//    
//    NSString *time = [NSString stringWithFormat:@"%d-%d-%d",year,month,day];
//    MyNSLog("%@",time);
//    return time;
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
